package com.deepseek.project.service.impl;

import com.deepseek.project.data.TalkInfoData;
import com.deepseek.project.data.TalkInfoDeepSeekHistoryData;
import com.deepseek.project.data.TalkInfoMessageData;
import com.deepseek.project.model.TalkInfo;
import com.deepseek.project.model.TalkInfoDeepSeekHistory;
import com.deepseek.project.model.TalkInfoMessage;
import com.deepseek.project.service.ITalkInfoService;
import com.google.gson.Gson;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * author: hulei
 * 对话框服务实现类
 */
@Slf4j
@Service
public class TalkInfoServiceImpl implements ITalkInfoService {

    @Resource
    private TalkInfoData talkInfoData;

    @Resource
    private TalkInfoMessageData talkInfoMessageData;

    @Resource
    private TalkInfoDeepSeekHistoryData talkInfoDeepSeekHistoryData;

    /**
     * 用于解析JSON数据的Gson实例
     */
    private static final Gson gson = new Gson();

    /**
     * 查询所有对话框列表
     */
    @Override
    public List<TalkInfo> talkInfoList() {
        return talkInfoData.list();
    }

    /**
     * 新增对话框
     */
    @Override
    public Integer saveTalkInfo(TalkInfo talkInfo) {
        talkInfo.setCreateTime(new java.util.Date());
        talkInfoData.save(talkInfo);
        Integer talkInfoId = talkInfo.getId();
        log.info("新增对话框成功，对话框ID为：{}", talkInfoId);
        return talkInfoId;
    }

    /**
     * 修改对话框
     */
    @Override
    public boolean updateTalkInfo(TalkInfo talkInfo) {
        return talkInfoData.updateById(talkInfo);
    }

    /**
     * 删除对话框
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteTalkInfo(TalkInfo talkInfo) {
        int talkInfoId = talkInfo.getId();
        talkInfoData.removeById(talkInfo);
        log.info("删除对话框成功，对话框ID为：{}", talkInfoId);
        talkInfoMessageData.deleteByTalkInfoId(talkInfoId);
        log.info("删除对话框内的历史消息成功，对话框ID为：{}", talkInfoId);
        talkInfoDeepSeekHistoryData.deleteByTalkInfoId(talkInfoId);
        return true;
    }
//======================================================================================================================
    /**
     * 查询单个对话框内的历史消息
     */
    @Override
    public List<TalkInfoMessage> getMessageListByTalkInfoId(Integer talkInfoId) {
        return talkInfoMessageData.getMessageListByTalkInfoId(talkInfoId);
    }

    /**
     * 新增对话框内的历史消息
     */
    @Override
    public boolean saveTalkInfoMessage(TalkInfoMessage talkInfoMessage) {
        talkInfoMessage.setCreateTime(new java.util.Date());
        return talkInfoMessageData.save(talkInfoMessage);
    }
//======================================================================================================================
    /**
     * 查询单个对话框内DeepSeek的历史消息
     */
    @Override
    public List<TalkInfoDeepSeekHistory> getDeepSeekHistoryListByTalkInfoId(Integer talkInfoId) {
        return talkInfoDeepSeekHistoryData.getHistoryListByTalkInfoId(talkInfoId);
    }

    /**
     * 对话框内DeepSeek的历史消息修改
     */
    @Override
    public void saveTalkInfoDeepSeekHistory(Integer talkInfoId, List<Map<String, String>> conversationHistory) {
        List<TalkInfoDeepSeekHistory> talkInfoDeepSeekHistoryList = new ArrayList<>();
        for (Map<String, String> map : conversationHistory) {
            TalkInfoDeepSeekHistory talkInfoDeepSeekHistory = new TalkInfoDeepSeekHistory();
            talkInfoDeepSeekHistory.setTalkInfoId(talkInfoId);
            talkInfoDeepSeekHistory.setContent(gson.toJson(map));
            talkInfoDeepSeekHistory.setTalkInfoId(talkInfoId);
            talkInfoDeepSeekHistory.setCreateTime(new java.util.Date());
            talkInfoDeepSeekHistoryList.add(talkInfoDeepSeekHistory);
        }
        talkInfoDeepSeekHistoryData.saveBatch(talkInfoDeepSeekHistoryList, 100);
    }

}
